package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.common.Result;
import com.zhentao.entity.Dynamic;
import com.zhentao.mapper.DynamicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 动态管理控制器
 */
@RestController
@RequestMapping("/admin/dynamic")
@CrossOrigin(origins = "*")
public class DynamicController {
    
    @Autowired
    private DynamicMapper dynamicMapper;
    
    /**
     * 动态列表（分页）
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer auditStatus) {
        
        try {
            Page<Dynamic> pageInfo = new Page<>(page, pageSize);
            QueryWrapper<Dynamic> queryWrapper = new QueryWrapper<>();
            
            // 审核状态筛选
            if (auditStatus != null) {
                queryWrapper.eq("audit_status", auditStatus);
            }
            
            // 按创建时间倒序
            queryWrapper.orderByDesc("create_time");
            
            Page<Dynamic> result = dynamicMapper.selectPage(pageInfo, queryWrapper);
            
            Map<String, Object> data = new HashMap<>();
            data.put("list", result.getRecords());
            data.put("total", result.getTotal());
            data.put("page", page);
            data.put("pageSize", pageSize);
            
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }
    
    /**
     * 动态详情
     */
    @GetMapping("/detail/{dynamicId}")
    public Result<Dynamic> detail(@PathVariable Integer dynamicId) {
        try {
            Dynamic dynamic = dynamicMapper.selectById(dynamicId);
            if (dynamic == null) {
                return Result.error("动态不存在");
            }
            return Result.success(dynamic);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }
    
    /**
     * 审核动态
     */
    @PostMapping("/audit/{dynamicId}")
    public Result<String> audit(@PathVariable Integer dynamicId, @RequestBody Map<String, Object> params) {
        try {
            Dynamic dynamic = dynamicMapper.selectById(dynamicId);
            if (dynamic == null) {
                return Result.error("动态不存在");
            }
            
            Integer auditStatus = (Integer) params.get("auditStatus");
            if (auditStatus == null) {
                return Result.error("审核状态不能为空");
            }
            
            dynamic.setAuditStatus(auditStatus);
            dynamicMapper.updateById(dynamic);
            
            return Result.success("审核成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("审核失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除动态
     */
    @DeleteMapping("/delete/{dynamicId}")
    public Result<String> delete(@PathVariable Integer dynamicId) {
        try {
            int rows = dynamicMapper.deleteById(dynamicId);
            if (rows > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("动态不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败：" + e.getMessage());
        }
    }
}

